package org.rzp.ideaplug.statistic.config;

import org.rzp.ideaplug.statistic.R;

import java.io.*;
import java.util.Properties;

public class PushConfig {

    private static String configFilePath;

    public static void loadConfig() {
        try {
            String userHomePath = System.getProperties().getProperty("user.home");
            File configFile = new File(userHomePath + File.separator + "pushInfoConfig.properties");
            if (!configFile.exists()) {
                configFile.createNewFile();
            }
            configFilePath = configFile.getPath();
            BufferedReader bufferedReader = new BufferedReader(new FileReader(configFile));
            Properties properties = new Properties();
            properties.load(bufferedReader);
            String url = properties.getProperty("url");
            R.url = url;
            String user = properties.getProperty("user");
            R.user = user;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void storeConfig(String url, String user) {
        Properties properties = new Properties();
        properties.setProperty("url", url);
        properties.setProperty("user", user);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(configFilePath);
            properties.store(fileOutputStream, "project or model java code file statistic push config");
            loadConfig();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fileOutputStream) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
